//引入四个常用通用样式
import "../../assets/css/index.less";
//引入当前样式表
import "../login/login.less";
//引入动画
require("animate.css");
//引入axios
import "../../assets/libs/axios.js";
//引入二次分装好的axios
import { postRegisterReq } from "../../api/user.js";
//引入节点封装函数
let dom = require("../../util/dom.js");
//引入提示框
import { toats } from "../../util/toats.js";
//引入验证表
var CaptchaMini = require("captcha-mini");
//captcha代码
let captcha1 = new CaptchaMini();
let codeNum = "";
captcha1.draw(document.querySelector("#captcha1"), (r) => {
	console.log(r, "验证码1");
	codeNum = r.toLowerCase();
});
//判断
dom.$("#btn").addEventListener("click", async () => {
	if (
		//手机号判断
		!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
			dom.$("#phone").value
		)
	) {
		return (dom.$("#feedback").innerHTML = "手机号不正确");
	}
	if (codeNum != dom.$("#verification").value) {
		//验证码判断
		return (dom.$("#feedback").innerHTML = "验证码不正确");
	}
	if (!/^(?=.*[a-zA-Z]).{1,9}/.test(dom.$("#password").value)) {
		//密码正则判断
		return (dom.$("#feedback").innerHTML = "至少包含1种至少包含字母,1-9位");
	}
	if (dom.$("#password").value !== dom.$("#ispassword").value) {
		//密码正则判断
		return (dom.$("#feedback").innerHTML = "两次输入的密码不一致");
	} else {
		//验证全部通过，开始清空提示并且发送ajax
		dom.$("#feedback").innerHTML = "";
		let res = await postRegisterReq({
			account: dom.$("#phone").value,
			password: dom.$("#password").value,
		});
		// console.log(res);检测是否成功
		//解构赋值
		let { message, errno } = res.data;
		if (errno === 0) {
			console.log("注册成功");
			toats("注册成功");
			setTimeout(() => {
				location.href = "./reg.html";
			}, 3000);
		} else {
			console.log(message);
			toats(message);
		}
	}
});
dom.$("#PassWordBtn").addEventListener("click", () => {
	location.href = "./reg.html";
});
